package com.adobesamples.controller.command
{
  import com.adobesamples.controller.events.DesignChosenEvent;
  import com.adobesamples.state.ModelState;
  import com.adobesamples.state.model;

  public class ChangeDesignCommand
  {
    private var _modelState: ModelState;

    public function ChangeDesignCommand(modelState: ModelState) {
      _modelState = modelState;
    }

    public function execute(event:DesignChosenEvent):void {
      var baseColor:Number = _modelState.currentConfiguration.baseColor;
      var trimColor:Number = _modelState.currentConfiguration.trimColor;

      _modelState.currentConfiguration.design = event.design.front;
      _modelState.currentConfiguration.designName = event.design.name;

      // Refreshes the baseColor within the flash shirt designs.
      if(_modelState.currentConfiguration.baseColor > 0) {
        _modelState.currentConfiguration.model::setBaseColor(
          _modelState.currentConfiguration.baseColor - 1
        );
      }
      else {
        _modelState.currentConfiguration.model::setBaseColor(
          _modelState.currentConfiguration.baseColor + 1
        );
      }

      // Refreshes the baseColor within the flash shirt designs.
      if(_modelState.currentConfiguration.trimColor > 0) {
        _modelState.currentConfiguration.model::setTrimColor(
          _modelState.currentConfiguration.trimColor - 1
        );
      }
      else {
        _modelState.currentConfiguration.model::setTrimColor(
          _modelState.currentConfiguration.trimColor + 1
        );
      }

      _modelState.currentConfiguration.model::setBaseColor(baseColor);
      _modelState.currentConfiguration.model::setTrimColor(trimColor);
    }

  }
}